﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace Kokomo.PeInspector.Reflection.Disassembly
{
	/// <summary>
	/// Represents a <see cref="MsilDependency"/> within the UI.
	/// </summary>
	public class MsilDependencyViewModel
	{
		/// <summary>
		/// Get the offset of the instruction causing the dependency.
		/// </summary>
		/// <seealso cref="MsilDependency.Offset"/>
		public int Offset { get; private set; }
		/// <summary>
		/// Gets the dependency.
		/// </summary>
		public object Target { get; private set; }
		/// <summary>
		/// Gets a textual description of the usage of the dependency.
		/// </summary>
		public string Usage { get; private set; }

		/// <summary>
		/// Gets the dependency as a <see cref="MemberInfo"/>.
		/// </summary>
		/// <seealso cref="Target"/>
		public MemberInfo TargetMember { get { return this.Target as MemberInfo; } }
		/// <summary>
		/// Gets the type declaring the dependency, if any.
		/// </summary>
		/// <see cref="TargetMember"/>
		public string TypeName
		{
			get
			{
				MemberInfo member = this.TargetMember;
				if (member == null) return null;
				Type type = member.ReflectedType;
				string typeName = (type == null) ? null : type.QualifiedFullName();
				return typeName;
			}
		}

		/// <summary>
		/// Initializes a new instance of <see cref="MsilDependencyViewModel"/>.
		/// </summary>
		/// <param name="offset">The offset of the instruction causing the dependency.</param>
		/// <param name="target">The dependency.</param>
		/// <param name="usage">A textual description of the usage of <paramref name="target"/>.</param>
		public MsilDependencyViewModel(
			int offset,
			object target,
			string usage = null
			)
		{
			this.Offset = offset;
			this.Target = target;
			this.Usage = usage;
		}
	}
}
